package authorizedDao

import (
	"fmt"
	"gitee.com/yangguoliang001/go-gin-vue3/QO"
	"gitee.com/yangguoliang001/go-gin-vue3/model"
	"gitee.com/yangguoliang001/go-gin-vue3/vo"
)

func (a *authorizedDao) QueryAuthList(params QO.AuthorizedListReq) (authListRes vo.AuthorizedListVO, err error) {
	var total int64
	var authList []model.AuthorizedDO
	err = a.db.Debug().Table("authorized").Where("business_key like ? AND business_developer like ? AND is_deleted = ? ", "%"+params.BusinessKey+"%", "%"+params.BusinessDeveloper+"%", -1).Count(&total).Limit(params.PageSize).Offset((params.Page - 1) * params.PageSize).Find(&authList).Error
	return vo.AuthorizedListVO{
		AuthList: authList,
		Total:    total,
		PageSize: params.PageSize,
		Page:     params.Page,
	}, err
}

// AddAuthorized 新增权限授权方
func (a *authorizedDao) AddAuthorized(authUser model.AuthorizedDO) (err error) {
	fmt.Println("authUser", authUser)
	err = a.db.Debug().Table("authorized").Create(&authUser).Error
	return err
}

// UpdateAuthorized 更新启用、禁用
func (a *authorizedDao) UpdateAuthorized(authReq QO.UpdateAuthorizedReq) (err error) {
	err = a.db.Debug().Table("authorized").Where("id = ?", authReq.Id).Update("is_used", authReq.Used).Error
	return
}

// DeleteAuthorized 删除
func (a *authorizedDao) DeleteAuthorized(id int) (err error) {
	err = a.db.Debug().Table("authorized").Where("id = ?", id).Update("is_deleted", 1).Error
	return
}
